package net.biancheng.controller;

import java.util.List;
import java.util.stream.Collectors;

import net.biancheng.common.constant.CommonConstant;
import net.biancheng.common.core.bean.TreeSelect;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.biancheng.common.annotation.Log;
import net.biancheng.common.core.BaseController;
import net.biancheng.common.core.CommonResult;
import net.biancheng.common.enums.BusinessType;
import net.biancheng.bean.SysMenu;
import net.biancheng.service.ISysMenuService;

/**
 * 菜单权限Controller
 *
 * @author lb
 * @date 2023-10-30
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController
{
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    public CommonResult list(SysMenu sysMenu)
    {
        List<SysMenu> list = sysMenuService.selectSysMenuList(sysMenu);
        return CommonResult.success(list);
    }

    /**
     * 查询菜单权限树形列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:treeList')")
    @GetMapping("/treeList")
    public CommonResult treeList(SysMenu sysMenu)
    {
        List<SysMenu> list = sysMenuService.selectSysMenuList(sysMenu);
        List<SysMenu> treeList = sysMenuService.buildMenuTree(list);
        return CommonResult.success(treeList);
    }

    /**
     * 查询菜单权限下拉树选择列表（目录+菜单，过滤按钮）
     */
    @PreAuthorize("@ss.hasPermi('system:menu:treeSelect')")
    @GetMapping("/treeSelect")
    public CommonResult treeSelect(SysMenu sysMenu)
    {
        List<SysMenu> list = sysMenuService.selectSysMenuList(sysMenu);
        //过滤按钮
        list = list.stream().filter(menu -> !CommonConstant.TYPE_BUTTON.equals(menu.getMenuType())).collect(Collectors.toList());
        List<TreeSelect> treeSelect = sysMenuService.buildMenuTreeSelect(list);
        return CommonResult.success(treeSelect);
    }

    /**
     * 获取菜单权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable Long id)
    {
        return CommonResult.success(sysMenuService.selectSysMenuById(id));
    }

    /**
     * 新增菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @Log(title = "菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody SysMenu sysMenu)
    {
        return toResult(sysMenuService.insertSysMenu(sysMenu));
    }

    /**
     * 修改菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @Log(title = "菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@RequestBody SysMenu sysMenu)
    {
        return toResult(sysMenuService.updateSysMenu(sysMenu));
    }

    /**
     * 删除菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @Log(title = "菜单权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public CommonResult remove(@PathVariable Long id)
    {
        return toResult(sysMenuService.deleteSysMenuById(id));
    }
}
